package topuc.com.mvp;

import android.Manifest;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.CoordinateConverter;
import com.amap.api.location.DPoint;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.route.DistanceItem;
import com.amap.api.services.route.DistanceResult;
import com.amap.api.services.route.DistanceSearch;

import java.util.ArrayList;
import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;
import topuc.com.mvp.fragment.EmotionFragment;
import topuc.com.mvp.view.CustomWheelView;
import topuc.com.mvp.view.PickerView;
import topuc.com.mvp.view.WheelView;

public class MainActivity extends AppCompatActivity{

    //    private EditText etText;
    private EmotionFragment emotionFragment;
    //    private RecyclerView recyclerView;
    private TextView tvClick;
    private PickerView pvMinute;
    private PickerView pvSecond;
    private WheelView mWheelView;
    private TextView tvStart;
    private CustomWheelView customWheel;

    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {

            Log.d("MainActivity", "city:" + aMapLocation.getCity());
            Log.d("MainActivity", "address:" + aMapLocation.getAddress());
            Log.d("MainActivity", "纬度:" + aMapLocation.getLatitude());
            Log.d("MainActivity", "经度:" + aMapLocation.getLongitude());

            LatLonPoint latLonPoint=new LatLonPoint(aMapLocation.getLatitude(),aMapLocation.getLongitude());

            getDistance("上海市浦东新区锦绣东路2015号",latLonPoint);
        }
    };

    /**
     * 根据位置的名称获取，到定位位置的距离
     *
     * @param locationName 起点位置的名称
     * @param latLonPoint  目的地的经纬度坐标
     */
    private void getDistance(String locationName,LatLonPoint latLonPoint) {

        List<LatLonPoint> mList=new ArrayList<>();
        mList.add(latLonPoint);

        GeocodeSearch geocodeSearch = new GeocodeSearch(this);
        GeocodeQuery geocodeQuery = new GeocodeQuery(locationName, "上海");
        geocodeSearch.getFromLocationNameAsyn(geocodeQuery);

        final DistanceSearch.DistanceQuery distanceQuery=new DistanceSearch.DistanceQuery();
        distanceQuery.setOrigins(mList);


        geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

            }

            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

                List<GeocodeAddress> mList = geocodeResult.getGeocodeAddressList();
                LatLonPoint latLonPoint = mList.get(0).getLatLonPoint();

                distanceQuery.setDestination(latLonPoint);
                distanceQuery.setType(DistanceSearch.TYPE_DRIVING_DISTANCE);

                DistanceSearch distanceSearch=new DistanceSearch(MainActivity.this);
                distanceSearch.calculateRouteDistanceAsyn(distanceQuery);

                distanceSearch.setDistanceSearchListener(new DistanceSearch.OnDistanceSearchListener() {
                    @Override
                    public void onDistanceSearched(DistanceResult distanceResult, int i) {

                        List<DistanceItem> mList=distanceResult.getDistanceResults();
                        float distance=mList.get(0).getDistance();

                        Log.d("MainActivity","distance:"+distance);
                    }
                });



            }
        });
//        geocoderSearch = new GeocodeSearch(this);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        tvStart = findViewById(R.id.tv_start);
        customWheel=findViewById(R.id.custom_wheel);

        List<String> mList=new ArrayList<String>();

        mList.add("a");
        mList.add("b");
        mList.add("c");
        mList.add("d");
        mList.add("e");
        mList.add("f");
        mList.add("g");
        mList.add("h");
        mList.add("i");
        mList.add("j");
        mList.add("k");
        mList.add("l");
        mList.add("m");
        mList.add("n");
        mList.add("o");
        mList.add("p");
        mList.add("q");
        mList.add("r");
        mList.add("s");
        mList.add("t");
        mList.add("u");
        mList.add("v");
        mList.add("w");
        mList.add("x");
        mList.add("y");
        mList.add("z");

        customWheel.setData(mList);

        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent();
                intent.setClass(MainActivity.this,ShowPositionActivity.class);
                startActivity(intent);
            }
        });

        initView();

//        initListener();
//        initData();
    }

    private void initView() {
        mWheelView = findViewById(R.id.wheelview);
        mWheelView.setData(getData1());
    }

    private boolean mIsData1 = true;

    /**
     * 重置数据
     *
     * @param view
     */
    public void resetData(View view) {
        if (mIsData1) {
            mWheelView.refreshData(getData2());
            mWheelView.setDefault(1);
        } else {
            mWheelView.refreshData(getData1());
        }
        mIsData1 = !mIsData1;
    }

    private ArrayList<String> getData1() {
        ArrayList<String> data = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            data.add(String.valueOf(((char) (97 + i))));
        }
        return data;
    }

    private ArrayList<String> getData2() {
        ArrayList<String> data = new ArrayList<>();
        data.add("A");
        data.add("B");
        data.add("C");
        return data;
    }

    private void initData() {

        Bundle bundle = new Bundle();
        //绑定编辑框
        bundle.putBoolean(EmotionFragment.BIND_TO_EDITTEXT, true);
        //隐藏控件
        bundle.putBoolean(EmotionFragment.HIDE_BAR_EDITTEXT_AND_BTN, false);

        emotionFragment = EmotionFragment.newInstance(EmotionFragment.class, bundle);
//        emotionFragment.bindToContentView(recyclerView);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_emotionview_main, emotionFragment);
        transaction.addToBackStack(null);
        transaction.commit();

    }

    private void initListener() {

        tvClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    private void initViews() {

        //etText=findViewById(R.id.et_text);
//        etText.setFocusable(true);
//        etText.setFocusableInTouchMode(true);
//        etText.requestFocus();

//        recyclerView = findViewById(R.id.recler);
        tvClick = findViewById(R.id.tv_click);

        pvMinute = findViewById(R.id.minute_pv);
        pvSecond = findViewById(R.id.second_pv);

        List<String> data = new ArrayList<String>();
        List<String> seconds = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            data.add("0" + i);
        }
        for (int i = 0; i < 60; i++) {
            seconds.add(i < 10 ? "0" + i : "" + i);
        }
        pvMinute.setData(data);
        pvMinute.setOnSelectListener(new PickerView.onSelectListener() {

            @Override
            public void onSelect(String text) {
                Toast.makeText(MainActivity.this, "选择了 " + text + " 分", Toast.LENGTH_SHORT).show();
            }
        });
        pvSecond.setData(seconds);
        pvSecond.setOnSelectListener(new PickerView.onSelectListener() {

            @Override
            public void onSelect(String text) {
                Toast.makeText(MainActivity.this, "选择了 " + text + " 秒",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}